package com.seaboxdata.util;


import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.validator.GenericValidator;

import java.util.HashMap;
import java.util.Map;

/***
 *
 *@author houcw
 *@date 2020-7-27
 **/
public class Sender {
    private static String url = "http://10.253.102.145:80/ebus/sharedataservice/szcy";////正式地址

    private static String testUrl = "";

    public static String send(boolean isProduct, String personKey) throws Exception{
        if (isProduct)
            return buildAndSend(url + "/" + personKey, isProduct);
        else
            return buildAndSend(testUrl + "/" + personKey, isProduct);
    }

    private static String buildAndSend(String url, boolean isProduct) throws Exception {
        return buildAndSend(url, isProduct, true, null);
    }

    private static String buildAndSend(String url, boolean isProduct, boolean isGet, String parmValue) throws Exception {
        Map<String, String> headerMap = new HashMap<String, String>();

        final String nonce = SignatureUtil.getNonce();
        final String timestampStr = SignatureUtil.getTs();

        String passId = SignatureUtil.PASSID;

        headerMap.put("x-tif-paasid", passId);
        headerMap.put("x-tif-signature", SignatureUtil.genSignature(timestampStr, nonce, isProduct));
        headerMap.put("x-tif-timestamp", timestampStr);
        headerMap.put("x-tif-nonce", nonce);

        headerMap.put("accept", "*/*");
        headerMap.put("connection", "Keep-Alive");
        headerMap.put("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
        headerMap.put("Content-Type", "application/json;charset=UTF-8");


        JSONObject obj = null;
        String result = null;
        result = HttpUtils.doGet(url, headerMap);
        if(StringUtils.isEmpty(result)){
            return null;
        }
        obj = JSONObject.parseObject(result);
        if ((obj.containsKey("message_type") || obj.containsKey("messageType"))
                && obj.containsKey("message")) {
            String decodeMsg = EncUtil.deCryptMsg(obj.getString("message"));
            if (!GenericValidator.isBlankOrNull(decodeMsg))
                return decodeMsg;
            else
                return obj.getString("message");
        }
        return result;
    }
}
